package com.spgf.controller;

import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.archtecture.model.exceptions.NegocioException;
import com.archtecture.model.patterns.PadraoActivityMnt;
import com.archtecture.model.patterns.PadraoRNAb;
import com.spgf.model.entities.OrcamentoModel;
import com.spgf.model.enums.Mes;
import com.spgf.model.rn.OrcamentoRN;
import com.spgf.view.utils.AdapterListViewOrcamento;
import com.spgf.view.utils.DialogManutencao;

public class OrcamentoMntActivity extends PadraoActivityMnt<OrcamentoModel> {

	private List<OrcamentoModel>	listaModel;

	private ListView				listview;

	@SuppressLint("SimpleDateFormat")
	public void atualizarLista() throws NegocioException {

		listaModel = null;

		Double valorTotal = 0.0;
		for (final OrcamentoModel l : getListaModel()) {
			valorTotal += l.getValor();
		}

		listview.setAdapter(new AdapterListViewOrcamento(this, getListaModel()));

	}

	@Override
	protected void construirTela(Bundle savedInstanceState) throws NegocioException {
		setContentView(R.layout.activity_orcamento_mnt);

		listview = (ListView) findViewById(R.id.lst_orcamento_mnt_resultado);
		listview.setScrollbarFadingEnabled(true);
		listview.setOnItemLongClickListener(this);

		// Mes lMes = Mes.values()[Calendar.getInstance().get(Calendar.MONTH)];
		// String lMesAno = lMes.getNome() + " / " + Calendar.getInstance().get(Calendar.YEAR);
		// ((TextView) findViewById(R.id.txt_despesa_mnt_titulo)).setText(lMesAno);

		atualizarLista();

	};

	protected boolean executarLongClick(AdapterView<?> pParent, View pView, int pPosition, long pID) throws NegocioException {

		if (pParent.getId() == R.id.lst_orcamento_mnt_resultado) {

			new DialogManutencao(this, getListaModel().get(pPosition)).show();
			return true;
		}

		return false;

	}

	@Override
	@SuppressWarnings({"rawtypes"})
	public Class getControleCadastro() {
		return OrcamentoCadActivity.class;
	}

	public List<OrcamentoModel> getListaModel() throws NegocioException {

		if (listaModel == null) {

			final OrcamentoModel model = new OrcamentoModel();

			Mes lMes = Mes.getEnum(Calendar.getInstance().get(Calendar.MONTH));

			listaModel = new OrcamentoRN(this).obterPorMes(model, lMes);
		}

		return listaModel;
	}

	@Override
	protected PadraoRNAb<OrcamentoModel> getRN() {
		return new OrcamentoRN(this);
	}
}
